//
//  CategoryController.m
//  ProductB
//
//  Created by 田阳 on 16/8/5.
//  Copyright © 2016年 田阳. All rights reserved.
//

#import "CategoryController.h"
#import "CategoryCell.h"
#import "FirstCell.h"
#import "CategoryModel.h"
#import "CategoryItemCell.h"
#import "ButtonView.h"
@interface CategoryController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property(nonatomic,strong) UICollectionView *collectionView;
@property(nonatomic,strong) UICollectionViewFlowLayout *flowLayout;

//存放分类界面所有数据字典
@property(nonatomic,strong) NSMutableDictionary *modelDic;

//将第一个cell设置为属性
@property(nonatomic,strong) CategoryItemCell *cell;
@end

@implementation CategoryController

- (NSMutableDictionary *)modelDic
{
    if (!_modelDic) {
        self.modelDic = [[NSMutableDictionary alloc] init];
    }
    return _modelDic;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self creatCollectionView];
    [self dataRequestWithURL];
}

- (void)creatCollectionView{
    self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
    _flowLayout.minimumLineSpacing = 0;
    _flowLayout.minimumInteritemSpacing = 0;
    _flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 114 - kScreenHeight / 20) collectionViewLayout:_flowLayout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.backgroundColor = [UIColor whiteColor];
    [_collectionView registerNib:[UINib nibWithNibName:@"CategoryCell" bundle:nil] forCellWithReuseIdentifier:@"categoryCell"];
    [_collectionView registerNib:[UINib nibWithNibName:@"FirstCell" bundle:nil] forCellWithReuseIdentifier:@"firstCell"];
    [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView"];
    [_collectionView registerNib:[UINib nibWithNibName:@"CategoryItemCell" bundle:nil] forCellWithReuseIdentifier:@"itemCell"];
    [self.view addSubview:_collectionView];
}

#pragma mark 数据解析
- (void)dataRequestWithURL{
    [NetWorkrequestManage requestWithType:GET url:CategoryClickURL parameters:nil finish:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        NSArray *array = dic[@"discoveryColumns"][@"list"];
        //存放图片下方Item
        NSMutableArray *titleArray = [[NSMutableArray alloc] init];
        for (NSDictionary *dict in array) {
            CategoryModel *model = [[CategoryModel alloc] init];
            [model setValuesForKeysWithDictionary:dict];
            [titleArray addObject:model];
        }
        [self.modelDic setValue:titleArray forKey:@"title"];
        
        NSArray *array1 = dic[@"list"];
        NSMutableArray *modelArray = [[NSMutableArray alloc] init];
        for (NSDictionary *dict in array1) {
            CategoryModel *model = [[CategoryModel alloc] init];
            [model setValuesForKeysWithDictionary:dict];
            [modelArray addObject:model];
        }
        [self.modelDic setValue:modelArray forKey:@"model"];
        if (self.modelDic.count == 2) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.collectionView reloadData];
            });
        }
    } error:nil];
}

#pragma mark ----colllectionView delegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return self.modelDic.count + 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    if (section == 0) {
        return 1;
    }else if (section == 1) {
        return 1;
    }else{
        NSArray *arr = self.modelDic[@"model"];
        return arr.count - 1;
    }
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView" forIndexPath:indexPath];
    headerView.alpha = 0.3;
    headerView.backgroundColor = [UIColor grayColor];
    return headerView;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    if (section == 1 || section == 0) {
        return CGSizeMake(0, 0);
    }else{
        return CGSizeMake(kScreenWidth, 10);
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return CGSizeMake(kScreenWidth, 150);
    }else if (indexPath.section == 1){
        return CGSizeMake(kScreenWidth, kScreenWidth / 5 + 30);
    }else{
        return CGSizeMake(kScreenWidth / 2, 40);
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        FirstCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"firstCell" forIndexPath:indexPath];
        NSArray *arr = self.modelDic[@"model"];
        CategoryModel *model = arr.firstObject;
        [cell.imageV sd_setImageWithURL:[NSURL URLWithString:model.coverPath]];
        return cell;
    }else if (indexPath.section == 1){
        self.cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"itemCell" forIndexPath:indexPath];
        [self setupItemCell];
        return _cell;
    }else{
        NSArray *arr = self.modelDic[@"model"];
        CategoryModel *model = arr[indexPath.row + 1];
        CategoryCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"categoryCell" forIndexPath:indexPath];
        [cell setDataWithModel:model];
        return cell;
    }
}

- (void)setupItemCell
{
    NSArray *arr = self.modelDic[@"title"];
    _cell.myScrollView.contentSize = CGSizeMake(arr.count * (kScreenWidth / 5 + 20) + 5, 0);
    _cell.myScrollView.showsHorizontalScrollIndicator = NO;
    
    for (NSInteger i = 0; i < arr.count; i++) {
        ButtonView *btnView = [[ButtonView alloc] initWithFrame:CGRectMake(5 + i * kScreenWidth / 5 + i * 20, 0, kScreenWidth / 5, kScreenWidth / 5 + 30)];
        CategoryModel *model = arr[i];
        [btnView.imageV sd_setImageWithURL:[NSURL URLWithString:model.coverPath]];
        
        btnView.label.text = model.title;
        [_cell.myScrollView addSubview:btnView];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];
        btnView.tag = i + 1;
        [btnView addGestureRecognizer:tap];
    }
}

- (void)tapClick:(UITapGestureRecognizer *)tap
{
    NSInteger index = tap.view.tag;
    NSLog(@"%ld",index);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}



@end
